/* $Id: BaseDAOImpl.java 
 */
package ae.pcfc.etks.online.ced.web.dao.provider;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;

import ae.pcfc.etks.online.ced.web.dao.BaseDAO;

public class BaseDAOImpl <T, PK extends Serializable> implements BaseDAO<T, PK> {

		protected Class<T> entityClass;

		
		@SuppressWarnings("unchecked")
		public BaseDAOImpl() {
			ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
			this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
		}

		public T getObject(Class<T> clazz, Serializable id) {

			return getHibernateTemplate().get(clazz, id);
		}

		@Autowired
		protected HibernateTemplate hibernateTemplate;

		@Autowired
		protected JdbcTemplate jdbcTemplate;

		public HibernateTemplate getHibernateTemplate() {
			return hibernateTemplate;
		}

		public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
			this.hibernateTemplate = hibernateTemplate;
		}

		public JdbcTemplate getJdbcTemplate() {
			return jdbcTemplate;
		}

		public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
			this.jdbcTemplate = jdbcTemplate;
		}

		public void removeObject(Object obj) {
			getHibernateTemplate().delete(obj);

		}
		@Override
		public void add(T persistentObject) {
			getHibernateTemplate().getSessionFactory().getCurrentSession().save(persistentObject);
		}

		@SuppressWarnings("unchecked")
		public List<T> findAll() {
			String all = "select x from " + entityClass.getSimpleName() + " x";
			return (List <T>)getHibernateTemplate().find(all);
		}
		
		public void remove(T persistentObject) {
			getHibernateTemplate().delete(persistentObject);

		}

		public void update(T persistentObject) {
			getHibernateTemplate().merge(persistentObject);
		}

}
